package com.ruipengkj.component;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.TexturePaint;

import javax.swing.JButton;

import com.ruipengkj.commons.util.Util;
import com.ruipengkj.factory.ImageFactory;

public class RPJButtonBlue extends JButton {

	private static final long serialVersionUID = 1L;
	
	
	private TexturePaint paint = Util.createTexturePaint(ImageFactory.loadImage(ImageFactory.BUTTON_BLUE_BG).getImage());
	private Image backgroundLeftImage = ImageFactory.loadImage(ImageFactory.BUTTON_BLUE_LEFT_BG).getImage();
	private Image backgroundRightImage = ImageFactory.loadImage(ImageFactory.BUTTON_BLUE_RIGHT_BG).getImage();
	
	private TexturePaint paint_select = Util.createTexturePaint(ImageFactory.loadImage(ImageFactory.BUTTON_BLUE_SELECT_BG).getImage());
	private Image backgroundLeftImage_select = ImageFactory.loadImage(ImageFactory.BUTTON_BLUE_SELECT_LEFT_BG).getImage();
	private Image backgroundRightImage_select = ImageFactory.loadImage(ImageFactory.BUTTON_BLUE_SELECT_RIGHT_BG).getImage();
	
	public RPJButtonBlue(String text){
		super(text);
		setContentAreaFilled(false); // 这一句非常重要, 否则父类还会绘制按钮的区域. 
		setForeground(Color.WHITE);
		setFont(Util.FONT_13_BOLD);
		setCursor(Util.CURSOR_HAND);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		if (getModel().isRollover()) {
			//鼠标滑过
			g2d.setPaint(paint_select);
			g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
			// draw left image.
			g2d.drawImage(backgroundLeftImage_select, 0, 0, null);
			// draw right image.
			int x = this.getWidth() - backgroundRightImage_select.getWidth(null);
			int y = 0;
			g2d.drawImage(backgroundRightImage_select, x, y, null);
		} else {
			g2d.setPaint(paint);
			g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
			// draw left image.
			g2d.drawImage(backgroundLeftImage, 0, 0, null);
			// draw right image.
			int x = this.getWidth() - backgroundRightImage.getWidth(null);
			int y = 0;
			g2d.drawImage(backgroundRightImage, x, y, null);
		}
		
//		g2d.dispose();
		super.paintComponent(g);
	}
}
